/**********************************************************
 * Test.java.java 
 * Created by Ashutosh
 * Last modified Date: Sep 18, 2008 11:03:01 AM by Ashutosh
 * Revision: <svn revision #> 
 * Version : 0.1
 * Copyright (c) 2008 - 2009 cyberThink Infotech Pvt. Ltd., All rights reserved.
 **********************************************************/

package com.printeverywhere.job;

import org.apache.log4j.Logger;
import java.util.Date;
import org.apache.struts2.impl.StrutsActionProxyFactory;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;
import com.opensymphony.xwork2.ActionProxy;

/**
  * The Class Test.
  */
 public class Test implements Job {
	
	/** Logger for this class. */
	private static final Logger logger = Logger.getLogger(Test.class);

	 /**
 	 * Instantiates a new test.
 	 */
 	public Test() {	 }

	 /* (non-Javadoc)
 	 * @see org.quartz.Job#execute(org.quartz.JobExecutionContext)
 	 */
 	public void execute(JobExecutionContext context) throws JobExecutionException {
		if (logger.isDebugEnabled()) {
			logger.debug("execute(JobExecutionContext) - start"); //$NON-NLS-1$
		}

		 try {
		 	/*
			 * HashMap ctx = new HashMap (); ctx.put
			 * (ActionContext.PARAMETERS, context.getJobDetail ().
			 * getJobDataMap ( )); Ctx.put
			 * (ComponentInterceptor.COMPONENT_MANAGER, ???); ctx.put
			 * (???,?) ServletDispatcher.createContextMap ()
			 */

			ActionProxy proxy = new StrutsActionProxyFactory().createActionProxy("", context.getJobDetail().getName(), null);
			proxy.execute();
		} catch (Exception e) {
			logger.error("execute(JobExecutionContext)", e); //$NON-NLS-1$

			throw new JobExecutionException (e);
		}

		if (logger.isDebugEnabled()) {
			logger.debug("execute(JobExecutionContext) - end"); //$NON-NLS-1$
		}
	 }
	 
 	/**
 	 * The main method.
 	 * 
 	 * @param args the arguments
 	 */
 	public final static void main (String [] args) {
		if (logger.isDebugEnabled()) {
			logger.debug("main(String[]) - start"); //$NON-NLS-1$
		}

		 try {
			 Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler ();
			 JobDetail jobDetail = new JobDetail ( "test", Scheduler.DEFAULT_GROUP, JTest.class);
			 long ctime = System.currentTimeMillis ();
			 SimpleTrigger simpleTrigger = new SimpleTrigger ( " simpleTrigger "," triggerGroup-s1 ");
			 simpleTrigger.setStartTime (new Date (ctime));
			 simpleTrigger.setRepeatInterval(2000);
			 simpleTrigger.setRepeatCount(100);
			 scheduler.scheduleJob(jobDetail, simpleTrigger);
			 scheduler.start ();
		 } catch (Exception e) {
			logger.error("main(String[])", e); //$NON-NLS-1$

			 e.printStackTrace();
		 }

		if (logger.isDebugEnabled()) {
			logger.debug("main(String[]) - end"); //$NON-NLS-1$
		}
	 }
}